package com.sanji.sjmallmodule.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.sanji.sjmallapp.BaseActivity;
import com.sanji.sjmallapp.R;
import com.sanji.sjmallmodule.main.ViewPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 导航页
 */
public class DaoHangActivity extends BaseActivity {
    @BindView(R.id.daohang_viewpager)
    ViewPager viewPager;
    private int[] drawables = {R.mipmap.daohang1, R.mipmap.daohang2};
    private List<ImageView> list;
    private ViewPagerAdapter viewPagerAdapter;
    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dao_hang);
        ButterKnife.bind(this);
        initView();
    }

    public void initView() {
        list = new ArrayList<>();
        for (int i = 0; i < drawables.length; i++) {
            ImageView view = new ImageView(this);
            view.setImageResource(drawables[i]);
            list.add(view);
        }
        viewPagerAdapter = new ViewPagerAdapter(list, mCtx);
        viewPager.setAdapter(viewPagerAdapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                if (position == drawables.length - 1) {
                    ImageView imageView = (ImageView) list.get(position);
                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            mHandler.removeCallbacks(run);
                            startActivity(new Intent(DaoHangActivity.this,
                                    LoginActivity.class));
                            finishActivity();
                        }
                    });
                    mHandler.postDelayed(run, 2000);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private Runnable run = new Runnable() {
        @Override
        public void run() {
            startActivity(new Intent(DaoHangActivity.this,
                    LoginActivity.class));
            finishActivity();
        }
    };
}